home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / debug / gdb / libiberty / strsignal.c < prev   
Encoding:
C/C++ Source or Header  |  1995-07-28  |  15.1 KB  |  644 lines

  1. /* Extended support for using signal values.
  2.    Copyright (C) 1992 Free Software Foundation, Inc.
  3.    Written by Fred Fish.  fnf@cygnus.com
  4.  
  5. This file is part of the libiberty library.
  6. Libiberty is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public
  8. License as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10.  
  11. Libiberty is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. Library General Public License for more details.
  15.  
  16. You should have received a copy of the GNU Library General Public
  17. License along with libiberty; see the file COPYING.LIB.  If
  18. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  19. Cambridge, MA 02139, USA.  */
  20.  
  21. #include "ansidecl.h"
  22. #include "libiberty.h"
  23.  
  24. #include "config.h"
  25.  
  26. #ifdef LOSING_SYS_SIGLIST
  27. #define sys_siglist no_such_symbol
  28. #endif
  29.  
  30. #include <stdio.h>
  31. #include <signal.h>
  32.  
  33. /*  Routines imported from standard C runtime libraries. */
  34.  
  35. #ifdef __STDC__
  36. #include <stddef.h>
  37. extern void *malloc (size_t size);                /* 4.10.3.3 */
  38. extern void *memset (void *s, int c, size_t n);            /* 4.11.6.1 */
  39. #else    /* !__STDC__ */
  40. extern char *malloc ();        /* Standard memory allocater */
  41. extern char *memset ();
  42. #endif    /* __STDC__ */
  43.  
  44. #ifdef LOSING_SYS_SIGLIST
  45. #undef sys_siglist
  46. #endif
  47.  
  48.  
  49. #ifndef NULL
  50. #  ifdef __STDC__
  51. #    define NULL (void *) 0
  52. #  else
  53. #    define NULL 0
  54. #  endif
  55. #endif
  56.  
  57. #ifndef MAX
  58. #  define MAX(a,b) ((a) > (b) ? (a) : (b))
  59. #endif
  60.  
  61. /* Translation table for signal values.
  62.  
  63.    Note that this table is generally only accessed when it is used at runtime
  64.    to initialize signal name and message tables that are indexed by signal
  65.    value.
  66.  
  67.    Not all of these signals will exist on all systems.  This table is the only
  68.    thing that should have to be updated as new signal numbers are introduced.
  69.    It's sort of ugly, but at least its portable. */
  70.  
  71. struct signal_info
  72. {
  73.   int value;        /* The numeric value from <signal.h> */
  74.   const char *name;    /* The equivalent symbolic value */
  75. #ifdef NEED_sys_siglist
  76.   const char *msg;    /* Short message about this value */
  77. #endif
  78. };
  79.  
  80. #ifdef NEED_sys_siglist
  81. #   define ENTRY(value, name, msg)    {value, name, msg}
  82. #else
  83. #   define ENTRY(value, name, msg)    {value, name}
  84. #endif
  85.  
  86. static const struct signal_info signal_table[] =
  87. {
  88. #if defined (SIGHUP)
  89.   ENTRY(SIGHUP, "SIGHUP", "Hangup"),
  90. #endif
  91. #if defined (SIGINT)
  92.   ENTRY(SIGINT, "SIGINT", "Interrupt"),
  93. #endif
  94. #if defined (SIGQUIT)
  95.   ENTRY(SIGQUIT, "SIGQUIT", "Quit"),
  96. #endif
  97. #if defined (SIGILL)
  98.   ENTRY(SIGILL, "SIGILL", "Illegal instruction"),
  99. #endif
  100. #if defined (SIGTRAP)
  101.   ENTRY(SIGTRAP, "SIGTRAP", "Trace/breakpoint trap"),
  102. #endif
  103. /* Put SIGIOT before SIGABRT, so that if SIGIOT==SIGABRT then SIGABRT
  104.    overrides SIGIOT.  SIGABRT is in ANSI and POSIX.1, and SIGIOT isn't. */
  105. #if defined (SIGIOT)
  106.   ENTRY(SIGIOT, "SIGIOT", "IOT trap"),
  107. #endif
  108. #if defined (SIGABRT)
  109.   ENTRY(SIGABRT, "SIGABRT", "Aborted"),
  110. #endif
  111. #if defined (SIGEMT)
  112.   ENTRY(SIGEMT, "SIGEMT", "Emulation trap"),
  113. #endif
  114. #if defined (SIGFPE)
  115.   ENTRY(SIGFPE, "SIGFPE", "Arithmetic exception"),
  116. #endif
  117. #if defined (SIGKILL)
  118.   ENTRY(SIGKILL, "SIGKILL", "Killed"),
  119. #endif
  120. #if defined (SIGBUS)
  121.   ENTRY(SIGBUS, "SIGBUS", "Bus error"),
  122. #endif
  123. #if defined (SIGSEGV)
  124.   ENTRY(SIGSEGV, "SIGSEGV", "Segmentation fault"),
  125. #endif
  126. #if defined (SIGSYS)
  127.   ENTRY(SIGSYS, "SIGSYS", "Bad system call"),
  128. #endif
  129. #if defined (SIGPIPE)
  130.   ENTRY(SIGPIPE, "SIGPIPE", "Broken pipe"),
  131. #endif
  132. #if defined (SIGALRM)
  133.   ENTRY(SIGALRM, "SIGALRM", "Alarm clock"),
  134. #endif
  135. #if defined (SIGTERM)
  136.   ENTRY(SIGTERM, "SIGTERM", "Terminated"),
  137. #endif
  138. #if defined (SIGUSR1)
  139.   ENTRY(SIGUSR1, "SIGUSR1", "User defined signal 1"),
  140. #endif
  141. #if defined (SIGUSR2)
  142.   ENTRY(SIGUSR2, "SIGUSR2", "User defined signal 2"),
  143. #endif
  144. /* Put SIGCLD before SIGCHLD, so that if SIGCLD==SIGCHLD then SIGCHLD
  145.    overrides SIGCLD.  SIGCHLD is in POXIX.1 */
  146. #if defined (SIGCLD)
  147.   ENTRY(SIGCLD, "SIGCLD", "Child status changed"),
  148. #endif
  149. #if defined (SIGCHLD)
  150.   ENTRY(SIGCHLD, "SIGCHLD", "Child status changed"),
  151. #endif
  152. #if defined (SIGPWR)
  153.   ENTRY(SIGPWR, "SIGPWR", "Power fail/restart"),
  154. #endif
  155. #if defined (SIGWINCH)
  156.   ENTRY(SIGWINCH, "SIGWINCH", "Window size changed"),
  157. #endif
  158. #if defined (SIGURG)
  159.   ENTRY(SIGURG, "SIGURG", "Urgent I/O condition"),
  160. #endif
  161. #if defined (SIGIO)
  162.   /* "I/O pending" has also been suggested, but is misleading since the
  163.      signal only happens when the process has asked for it, not everytime
  164.      I/O is pending. */
  165.   ENTRY(SIGIO, "SIGIO", "I/O possible"),
  166. #endif
  167. #if defined (SIGPOLL)
  168.   ENTRY(SIGPOLL, "SIGPOLL", "Pollable event occurred"),
  169. #endif
  170. #if defined (SIGSTOP)
  171.   ENTRY(SIGSTOP, "SIGSTOP", "Stopped (signal)"),
  172. #endif
  173. #if defined (SIGTSTP)
  174.   ENTRY(SIGTSTP, "SIGTSTP", "Stopped (user)"),
  175. #endif
  176. #if defined (SIGCONT)
  177.   ENTRY(SIGCONT, "SIGCONT", "Continued"),
  178. #endif
  179. #if defined (SIGTTIN)
  180.   ENTRY(SIGTTIN, "SIGTTIN", "Stopped (tty input)"),
  181. #endif
  182. #if defined (SIGTTOU)
  183.   ENTRY(SIGTTOU, "SIGTTOU", "Stopped (tty output)"),
  184. #endif
  185. #if defined (SIGVTALRM)
  186.   ENTRY(SIGVTALRM, "SIGVTALRM", "Virtual timer expired"),
  187. #endif
  188. #if defined (SIGPROF)
  189.   ENTRY(SIGPROF, "SIGPROF", "Profiling timer expired"),
  190. #endif
  191. #if defined (SIGXCPU)
  192.   ENTRY(SIGXCPU, "SIGXCPU", "CPU time limit exceeded"),
  193. #endif
  194. #if defined (SIGXFSZ)
  195.   ENTRY(SIGXFSZ, "SIGXFSZ", "File size limit exceeded"),
  196. #endif
  197. #if defined (SIGWIND)
  198.   ENTRY(SIGWIND, "SIGWIND", "SIGWIND"),
  199. #endif
  200. #if defined (SIGPHONE)
  201.   ENTRY(SIGPHONE, "SIGPHONE", "SIGPHONE"),
  202. #endif
  203. #if defined (SIGLOST)
  204.   ENTRY(SIGLOST, "SIGLOST", "Resource lost"),
  205. #endif
  206. #if defined (SIGWAITING)
  207.   ENTRY(SIGWAITING, "SIGWAITING", "Process's LWPs are blocked"),
  208. #endif
  209. #if defined (SIGLWP)
  210.   ENTRY(SIGLWP, "SIGLWP", "Signal LWP"),
  211. #endif
  212. #if defined (SIGDANGER)
  213.   ENTRY(SIGDANGER, "SIGDANGER", "Swap space dangerously low"),
  214. #endif
  215. #if defined (SIGGRANT)
  216.   ENTRY(SIGGRANT, "SIGGRANT", "Monitor mode granted"),
  217. #endif
  218. #if defined (SIGRETRACT)
  219.   ENTRY(SIGRETRACT, "SIGRETRACT", "Need to relinguish monitor mode"),
  220. #endif
  221. #if defined (SIGMSG)
  222.   ENTRY(SIGMSG, "SIGMSG", "Monitor mode data available"),
  223. #endif
  224. #if defined (SIGSOUND)
  225.   ENTRY(SIGSOUND, "SIGSOUND", "Sound completed"),
  226. #endif
  227. #if defined (SIGSAK)
  228.   ENTRY(SIGSAK, "SIGSAK", "Secure attention"),
  229. #endif
  230.   ENTRY(0, NULL, NULL)
  231. };
  232.  
  233. /* Translation table allocated and initialized at runtime.  Indexed by the
  234.    signal value to find the equivalent symbolic value. */
  235.  
  236. static const char **signal_names;
  237. static int num_signal_names = 0;
  238.  
  239. /* Translation table allocated and initialized at runtime, if it does not
  240.    already exist in the host environment.  Indexed by the signal value to find
  241.    the descriptive string.
  242.  
  243.    We don't export it for use in other modules because even though it has the
  244.    same name, it differs from other implementations in that it is dynamically
  245.    initialized rather than statically initialized. */
  246.  
  247. #ifdef NEED_sys_siglist
  248.  
  249. static int sys_nsig;
  250. static const char **sys_siglist;
  251.  
  252. #else
  253.  
  254. static int sys_nsig = NSIG;
  255. extern const char * const sys_siglist[];
  256.  
  257. #endif
  258.  
  259.  
  260. /*
  261.  
  262. NAME
  263.  
  264.     init_signal_tables -- initialize the name and message tables
  265.  
  266. SYNOPSIS
  267.  
  268.     static void init_signal_tables ();
  269.  
  270. DESCRIPTION
  271.  
  272.     Using the signal_table, which is initialized at compile time, generate
  273.     the signal_names and the sys_siglist (if needed) tables, which are
  274.     indexed at runtime by a specific signal value.
  275.  
  276. BUGS
  277.  
  278.     The initialization of the tables may fail under low memory conditions,
  279.     in which case we don't do anything particularly useful, but we don't
  280.     bomb either.  Who knows, it might succeed at a later point if we free
  281.     some memory in the meantime.  In any case, the other routines know
  282.     how to deal with lack of a table after trying to initialize it.  This
  283.     may or may not be considered to be a bug, that we don't specifically
  284.     warn about this particular failure mode.
  285.  
  286. */
  287.  
  288. static void
  289. init_signal_tables ()
  290. {
  291.   const struct signal_info *eip;
  292.   int nbytes;
  293.  
  294.   /* If we haven't already scanned the signal_table once to find the maximum
  295.      signal value, then go find it now. */
  296.  
  297.   if (num_signal_names == 0)
  298.     {
  299.       for (eip = signal_table; eip -> name != NULL; eip++)
  300.     {
  301.       if (eip -> value >= num_signal_names)
  302.         {
  303.           num_signal_names = eip -> value + 1;
  304.         }
  305.     }
  306.     }
  307.  
  308.   /* Now attempt to allocate the signal_names table, zero it out, and then
  309.      initialize it from the statically initialized signal_table. */
  310.  
  311.   if (signal_names == NULL)
  312.     {
  313.       nbytes = num_signal_names * sizeof (char *);
  314.       if ((signal_names = (const char **) malloc (nbytes)) != NULL)
  315.     {
  316.       memset (signal_names, 0, nbytes);
  317.       for (eip = signal_table; eip -> name != NULL; eip++)
  318.         {
  319.           signal_names[eip -> value] = eip -> name;
  320.         }
  321.     }
  322.     }
  323.  
  324. #ifdef NEED_sys_siglist
  325.  
  326.   /* Now attempt to allocate the sys_siglist table, zero it out, and then
  327.      initialize it from the statically initialized signal_table. */
  328.  
  329.   if (sys_siglist == NULL)
  330.     {
  331.       nbytes = num_signal_names * sizeof (char *);
  332.       if ((sys_siglist = (const char **) malloc (nbytes)) != NULL)
  333.     {
  334.       memset (sys_siglist, 0, nbytes);
  335.       sys_nsig = num_signal_names;
  336.       for (eip = signal_table; eip -> name != NULL; eip++)
  337.         {
  338.           sys_siglist[eip -> value] = eip -> msg;
  339.         }
  340.     }
  341.     }
  342.  
  343. #endif
  344.  
  345. }
  346.  
  347.  
  348. /*
  349.  
  350. NAME
  351.  
  352.     signo_max -- return the max signo value
  353.  
  354. SYNOPSIS
  355.  
  356.     int signo_max ();
  357.  
  358. DESCRIPTION
  359.  
  360.     Returns the maximum signo value for which a corresponding symbolic
  361.     name or message is available.  Note that in the case where
  362.     we use the sys_siglist supplied by the system, it is possible for
  363.     there to be more symbolic names than messages, or vice versa.
  364.     In fact, the manual page for psignal(3b) explicitly warns that one
  365.     should check the size of the table (NSIG) before indexing it,
  366.     since new signal codes may be added to the system before they are
  367.     added to the table.  Thus NSIG might be smaller than value
  368.     implied by the largest signo value defined in <signal.h>.
  369.  
  370.     We return the maximum value that can be used to obtain a meaningful
  371.     symbolic name or message.
  372.  
  373. */
  374.  
  375. int
  376. signo_max ()
  377. {
  378.   int maxsize;
  379.  
  380.   if (signal_names == NULL)
  381.     {
  382.       init_signal_tables ();
  383.     }
  384.   maxsize = MAX (sys_nsig, num_signal_names);
  385.   return (maxsize - 1);
  386. }
  387.  
  388.  
  389. /*
  390.  
  391. NAME
  392.  
  393.     strsignal -- map a signal number to a signal message string
  394.  
  395. SYNOPSIS
  396.  
  397.     const char *strsignal (int signo)
  398.  
  399. DESCRIPTION
  400.  
  401.     Maps an signal number to an signal message string, the contents of
  402.     which are implementation defined.  On systems which have the external
  403.     variable sys_siglist, these strings will be the same as the ones used
  404.     by psignal().
  405.  
  406.     If the supplied signal number is within the valid range of indices
  407.     for the sys_siglist, but no message is available for the particular
  408.     signal number, then returns the string "Signal NUM", where NUM is the
  409.     signal number.
  410.  
  411.     If the supplied signal number is not a valid index into sys_siglist,
  412.     returns NULL.
  413.  
  414.     The returned string is only guaranteed to be valid only until the
  415.     next call to strsignal.
  416.  
  417. */
  418.  
  419. const char *
  420. strsignal (signo)
  421.   int signo;
  422. {
  423.   const char *msg;
  424.   static char buf[32];
  425.  
  426. #ifdef NEED_sys_siglist
  427.  
  428.   if (signal_names == NULL)
  429.     {
  430.       init_signal_tables ();
  431.     }
  432.  
  433. #endif
  434.  
  435.   if ((signo < 0) || (signo >= sys_nsig))
  436.     {
  437.       /* Out of range, just return NULL */
  438.       msg = NULL;
  439.     }
  440.   else if ((sys_siglist == NULL) || (sys_siglist[signo] == NULL))
  441.     {
  442.       /* In range, but no sys_siglist or no entry at this index. */
  443.       sprintf (buf, "Signal %d", signo);
  444.       msg = (const char *) buf;
  445.     }
  446.   else
  447.     {
  448.       /* In range, and a valid message.  Just return the message. */
  449.       msg = (const char *) sys_siglist[signo];
  450.     }
  451.   
  452.   return (msg);
  453. }
  454.  
  455.  
  456. /*
  457.  
  458. NAME
  459.  
  460.     strsigno -- map an signal number to a symbolic name string
  461.  
  462. SYNOPSIS
  463.  
  464.     const char *strsigno (int signo)
  465.  
  466. DESCRIPTION
  467.  
  468.     Given an signal number, returns a pointer to a string containing
  469.     the symbolic name of that signal number, as found in <signal.h>.
  470.  
  471.     If the supplied signal number is within the valid range of indices
  472.     for symbolic names, but no name is available for the particular
  473.     signal number, then returns the string "Signal NUM", where NUM is
  474.     the signal number.
  475.  
  476.     If the supplied signal number is not within the range of valid
  477.     indices, then returns NULL.
  478.  
  479. BUGS
  480.  
  481.     The contents of the location pointed to are only guaranteed to be
  482.     valid until the next call to strsigno.
  483.  
  484. */
  485.  
  486. const char *
  487. strsigno (signo)
  488.   int signo;
  489. {
  490.   const char *name;
  491.   static char buf[32];
  492.  
  493.   if (signal_names == NULL)
  494.     {
  495.       init_signal_tables ();
  496.     }
  497.  
  498.   if ((signo < 0) || (signo >= num_signal_names))
  499.     {
  500.       /* Out of range, just return NULL */
  501.       name = NULL;
  502.     }
  503.   else if ((signal_names == NULL) || (signal_names[signo] == NULL))
  504.     {
  505.       /* In range, but no signal_names or no entry at this index. */
  506.       sprintf (buf, "Signal %d", signo);
  507.       name = (const char *) buf;
  508.     }
  509.   else
  510.     {
  511.       /* In range, and a valid name.  Just return the name. */
  512.       name = signal_names[signo];
  513.     }
  514.  
  515.   return (name);
  516. }
  517.  
  518.  
  519. /*
  520.  
  521. NAME
  522.  
  523.     strtosigno -- map a symbolic signal name to a numeric value
  524.  
  525. SYNOPSIS
  526.  
  527.     int strtosigno (char *name)
  528.  
  529. DESCRIPTION
  530.  
  531.     Given the symbolic name of a signal, map it to a signal number.
  532.     If no translation is found, returns 0.
  533.  
  534. */
  535.  
  536. int
  537. strtosigno (name)
  538.      const char *name;
  539. {
  540.   int signo = 0;
  541.  
  542.   if (name != NULL)
  543.     {
  544.       if (signal_names == NULL)
  545.     {
  546.       init_signal_tables ();
  547.     }
  548.       for (signo = 0; signo < num_signal_names; signo++)
  549.     {
  550.       if ((signal_names[signo] != NULL) &&
  551.           (strcmp (name, signal_names[signo]) == 0))
  552.         {
  553.           break;
  554.         }
  555.     }
  556.       if (signo == num_signal_names)
  557.     {
  558.       signo = 0;
  559.     }
  560.     }
  561.   return (signo);
  562. }
  563.  
  564.  
  565. /*
  566.  
  567. NAME
  568.  
  569.     psignal -- print message about signal to stderr
  570.  
  571. SYNOPSIS
  572.  
  573.     void psignal (unsigned signo, char *message);
  574.  
  575. DESCRIPTION
  576.  
  577.     Print to the standard error the message, followed by a colon,
  578.     followed by the description of the signal specified by signo,
  579.     followed by a newline.
  580. */
  581.  
  582. #ifdef NEED_psignal
  583.  
  584. void
  585. psignal (signo, message)
  586.   unsigned signo;
  587.   char *message;
  588. {
  589.   if (signal_names == NULL)
  590.     {
  591.       init_signal_tables ();
  592.     }
  593.   if ((signo <= 0) || (signo >= sys_nsig))
  594.     {
  595.       fprintf (stderr, "%s: unknown signal\n", message);
  596.     }
  597.   else
  598.     {
  599.       fprintf (stderr, "%s: %s\n", message, sys_siglist[signo]);
  600.     }
  601. }
  602.  
  603. #endif    /* NEED_psignal */
  604.  
  605.  
  606. /* A simple little main that does nothing but print all the signal translations
  607.    if MAIN is defined and this file is compiled and linked. */
  608.  
  609. #ifdef MAIN
  610.  
  611. #include <stdio.h>
  612.  
  613. int
  614. main ()
  615. {
  616.   int signo;
  617.   int maxsigno;
  618.   const char *name;
  619.   const char *msg;
  620.  
  621.   maxsigno = signo_max ();
  622.   printf ("%d entries in names table.\n", num_signal_names);
  623.   printf ("%d entries in messages table.\n", sys_nsig);
  624.   printf ("%d is max useful index.\n", maxsigno);
  625.  
  626.   /* Keep printing values until we get to the end of *both* tables, not
  627.      *either* table.  Note that knowing the maximum useful index does *not*
  628.      relieve us of the responsibility of testing the return pointer for
  629.      NULL. */
  630.  
  631.   for (signo = 0; signo <= maxsigno; signo++)
  632.     {
  633.       name = strsigno (signo);
  634.       name = (name == NULL) ? "<NULL>" : name;
  635.       msg = strsignal (signo);
  636.       msg = (msg == NULL) ? "<NULL>" : msg;
  637.       printf ("%-4d%-18s%s\n", signo, name, msg);
  638.     }
  639.  
  640.   return 0;
  641. }
  642.  
  643. #endif
  644.